package com.example.kingdee.sqlTool.common;




import cn.hutool.core.date.DateUtil;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.sqlTool.sqltoolEnum.FormIdEnum;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class QueryObject extends SwMap {

    private ArrayList<String> selectArray;
    private ConditionMap condition;
    private OrderMap order;

    public QueryObject() {
    }

    public QueryObject(String formId) {
        setFormId(formId);
    }

    public QueryObject(FormIdEnum formIdEnum) {
        setFormId(FormIdEnum.getDBTableName(formIdEnum.name()));
    }

    public String getFormId() {
        return (String) get("FormId");
    }

    public void setFormId(String formId) {
        put("FormId",formId);
    }
    /**
     * 添加查询字段
     * @return
     */
    public QueryObject addSelect(String... selectList){
        ArrayList<String> selectArray = Optional.ofNullable(getSelectArray()).orElse(new ArrayList<>());
        for (String s : selectList) {
            selectArray.add(s);
        }
        setSelectArray(selectArray);
        return this;
    }

    public ArrayList<String> getSelectArray() {
        return Optional.ofNullable((ArrayList<String>)get("selectArray")).orElse(new ArrayList<>());
    }

    public void setSelectArray(ArrayList<String> selectArray) {
        put("selectArray",selectArray);
    }

    /**
     * 添加查询条件
     * @return
     */
    public QueryObject addCondition(String conditionKey,Object conditionValue){
        ConditionMap condition = Optional.ofNullable(getCondition()).orElse(new ConditionMap());
        condition.put(conditionKey,conditionValue);
        setCondition(condition);
        return this;
    }

    public ConditionMap getCondition() {
        return ConditionMap.parseOfHashMap(Optional.ofNullable((HashMap<String,Object>)get("condition")).orElse(new HashMap<>()) );
    }

    public void setCondition(ConditionMap condition) {
        put("condition",condition);
    }

    /**
     * 添加顺序排序
     * @return
     */
    public QueryObject addOrder(String OrderString){
        setOrder(OrderString);
        return this;
    }

    /**
     * 添加搜索条数
     * @return
     */
    public QueryObject addLimit(long limit){
        setLimit(limit);
        return this;
    }

    /**
     * 汇总表添加搜索条数
     * @return
     */
    public QueryObject addModel(HashMap<String, Object> model){
        setModel(model);
        return this;
    }


    public void setOrder(String orderString) {
        put("OrderString",orderString);
    }

    public void setLimit(long limit) {
        put("limit",limit);
    }

    public void setModel(HashMap<String, Object> model) {
        put("Model",model);
    }

    /**
     * 添加表单所在的子系统内码
     * @return
     */
    public QueryObject addSubSystemId(String subSystemId){
        setSubSystemId(subSystemId);
        return this;
    }

    public void setSubSystemId(String subSystemId) {
        put("SubSystemId",subSystemId);
    }
    /**
     * 添加页码信息
     * @return
     */
    public QueryObject addPage(Integer pageIndex,Integer pageSize, Integer limit){
        setPageIndex(pageIndex);
        setPageSize(pageSize);
        setLimit(limit);
        return this;
    }

    /**
     * 汇总表查询
     * @return
     */
    public HashMap<String, Object> getModel(){
        String now = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        Date date = DateUtil.date();
        //获得年的部分
        int year = DateUtil.year(date);
        //获得月份，从0开始计数
        int month = DateUtil.month(date);
        HashMap<String, Object> model = new LinkedHashMap<>();
        model.put("FENDYEAR", Integer.toString(year));//结束年份
        model.put("FSTARTYEAR", Integer.toString(year));//开始年份
        model.put("FENDPERIOD", Integer.toString(month));//结束月份
        model.put("FSTARTPERIOD", Integer.toString(month));//开始月份
        model.put("FInSettle", false);//内部结算
        model.put("FOutSettle", true);//外部结算
        model.put("FNoShowForBoth", false);//无发生额且余额为零不显示
        model.put("FSettleOrgLst", "1");//结算组织
        model.put("FEndDate", now);//结束时间
        model.put("FBeginDate", now);//开始时间
        model.put("FNoShowForNoLeft", false);//余额为零不显示
        model.put("FNoShowForNoOccur", false);//无发生额不显示
        model.put("FNOAUDIT", false);//包括未审核单据
        model.put("FIncludePayEvaluate_New", false);//包含暂估应收
        model.put("FIncludePayEvaluate", false);//包含已出库未应收
        model.put("FOnlyShowPayEvaluate_New", false);//仅显示暂估应收
        model.put("FOnlyShowPayEvaluate", false);//仅显示已出库未应收
        model.put("FDEFAULTACCTCALENDARID", false);//默认会计日历
        model.put("FShowLocal", false);//显示本位币
        model.put("FShowSumLocal", false);//显示综合本位币小计
        model.put("FGroupCustomer", false);//集团客户
        model.put("FNoPreReceive", false);//不包含预收款
        model.put("FOnlyShowPreReceive", false);//仅显示预收款
        model.put("FCONTACTUNITMUL", "");//往来单位集合，样例数据如 "C0005,C0006,C0007,C0008"
        model.put("FMULCONTACT", true);//多选
        model.put("FEXCLUDEB2CAR", false);//不包含B2C单据
        model.put("FDateRadioGrp", 1);//单选按钮组
        return model;
    }

    public Integer getPageIndex() {
        return (Integer) get("pageIndex");
    }

    public void setPageIndex(Integer pageIndex) {
        put("pageIndex",pageIndex);
    }

    public Integer getPageSize() {
        return (Integer) get("pageSize");
    }

    public void setPageSize(Integer pageSize) {
        put("pageSize",pageSize);
    }

    public Integer getLimit() {
        return (Integer) get("limit");
    }

    public void setLimit(Integer limit) {
        put("limit",limit);
    }

    public void initParam(){

        if (this.get("FormId") == null){
            throw new ProcedureException("表单id不可为空");
        }

        if (this.get("FieldKeys") == null){
            throw new ProcedureException("查询字段不可为空");
        }

        this.putIfAbsent("FilterString", "1=1");
    }
}
